package com.buba.system.controller;

import com.alibaba.fastjson.JSON;
import com.buba.system.pojo.UserInfo;
import com.buba.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
    @Override
    public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag = request.getParameter("flag");
        if ("selloginUser".equals(flag));
            this.selloginUser(request,response);
    }

    private void selloginUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        System.out.println("获取登录用户");
        // 从session域中获取登录的用户
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute("login");
        if (user != null){
            response.getWriter().write(JSON.toJSONString(user));
        }
    }
}
